package com.itzmn.newcode.offer;


/**
 * @Author: 张梦楠
 * @Date: 2018/11/20 16:46
 * 简书：https://www.jianshu.com/u/d611be10d1a6
 * 码云：https://gitee.com/zhangqiye
 * @Description:  剑指Offer 将字符串中的字符左移几位，得到新的字符串
 *
 *   思路：
 *      生成新数组，然后左移，从第一位开始左移，如果左移之后的位置小于0，则长度加索引-位移
 */
public class LeftRoatString {

    public static void main(String[] args) {
        new LeftRoatString().init();
    }

    public void init(){

        LeftRotateString("abcdefg",2);
    }

    public String LeftRotateString(String str,int n) {

        if (str.equals("")){
            return "";
        }


        String[] split = str.split("");

        String[] strings = new String[split.length];

        for (int i=0 ;i<split.length;i++){

            String s = split[i];

            if (i-n<0){
                strings[i+split.length-n] = s;
            }else {
                strings[i-n] = s;
            }

        }
        StringBuilder sb = new StringBuilder();
        for (String s:strings){
            sb.append(s);
        }

        System.out.println(sb.toString());
        return sb.toString();
    }
}
